package org.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.lang.ClassNotFoundException;
/**
 * @author xiaomengzi
 * @create 2025-08-04 下午2:49
 */
public class DBConnection {
    private static final String URL = "jdbc:mysql://localhost:3306/api_db?useSSL=false&serverTimezone=UTC";
    private static final String USER = "api_user";
    private static final String PASSWORD = "api_password"; // 替换为你的数据库密码

    private static Connection connection;
    private DBConnection(){}
    public static Connection getConnection() throws SQLException {
        if (connection == null || connection.isClosed()) {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                connection = DriverManager.getConnection(URL, USER, PASSWORD);
            } catch (ClassNotFoundException e) {
                throw new SQLException("MySQL JDBC驱动未找到"+e);
            }
        }
        return connection;
    }

    public static void closeConnection() {
        if (connection != null) {
            try {
                connection.close();
                connection = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
